<?php

class Lite_Collection_ArraySet 
	extends Lite_Collection_AbstractCollection
	implements Lite_Collection_Set {
	
	/**
	 * @param LiteCollection $colA
	 * @param LiteCollection $colB
	 * @return LiteCollection
	 */
	static function union(LiteCollection $colA, LiteCollection $colB){
		return self::unionInto(new self, $colA, $colB);
	}

	/**
	 * El conjunto de elementos
	 * 
	 * @var array
	 */
	private $elements = array();
	
	/**
	 * 
	 */
	public function clear(){
		$this->elements = array(); 
	}

	/**
	 * @param mixed $element
	 */
	public function add($element){
		$this->remove( $element );
		$this->elements[] = $element;
	}

	/**
	 * @param mixed $element
	 * @return boolean
	 */
	public function contains($element){
		return false !== $this->find($element); 
	}

	/**
	 * 
	 * @param mixed $element
	 * @return int posicion del elemento o <code>false</code> en caso de no pertenecer
	 */
	private function find($element) {
		return array_search($element, $this->elements, true);
	} 
	
	/**
	 * @param mixed $element
	 */
	public function remove($element){
		if( false !== ($index = $this->find($element))){
			array_splice($this->elements, $index, 1);
		}
	}

	/**
	 * @return array 
	 */
	public function toArray(){
		return $this->elements;
	}
}